function out = waldTestRegimeShift_perStateVariable(theta2,AVarTheta2,nx)

% Test for change in intercepts if possible
if any(strcmp('h01_2',AVarTheta2.names))
    numTheta2 = length(AVarTheta2.names);
    for i=1:nx
        pos_h0_1(:,1) = strcmp(['h0',num2str(i),'_1'],AVarTheta2.names);
        pos_h0_2(:,1) = strcmp(['h0',num2str(i),'_2'],AVarTheta2.names);
        R = (pos_h0_1-pos_h0_2)';
        if numTheta2 ~= length(fieldnames(theta2))
            theta2Test = reduceTheta2(theta2,AVarTheta2.names);
        else
            theta2Test = theta2;
        end
        theta2TestValues = struct2array(theta2Test)';
        Q_intercept_eq(i,1) = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
        pValue_intercept_eq(i,1) = chi2cdf(Q_intercept_eq(i,1),1,'upper');
    end    
else
    Q_intercept_eq = NaN;
    pValue_intercept_eq = NaN;
end

% Test for change in slopes
if any(strcmp('hx11_2',AVarTheta2.names))
    numTheta2 = length(AVarTheta2.names);
    for i=1:nx
        idx      = 0;
        pos_hx_1 = zeros(numTheta2,nx);
        pos_hx_2 = zeros(numTheta2,nx);
        for j=1:nx
            idx = idx + 1;
            pos_hx_1(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
            pos_hx_2(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
        end
        R = (pos_hx_1-pos_hx_2)';
        if numTheta2 ~= length(fieldnames(theta2))
            theta2Test = reduceTheta2(theta2,AVarTheta2.names);
        else
            theta2Test = theta2;
        end
        theta2TestValues = struct2array(theta2Test)';
        Q_slope_eq(i,1) = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
        pValue_slope_eq(i,1) = chi2cdf(Q_slope_eq(i,1),nx,'upper');
    end
else
    Q_slope_eq = NaN;
    pValue_slope_eq = NaN;
end

% The output
out.Q_intercept_eq      = Q_intercept_eq;
out.pValue_intercept_eq = pValue_intercept_eq;
out.Q_slope_eq          = Q_slope_eq;
out.pValue_slope_eq     = pValue_slope_eq;
